/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.osdtsystem.hierarchy.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import org.osdtsystem.hierarchy.Hierarchy;
import org.osdtsystem.hierarchy.PrefixHierarchy;
import org.osdtsystem.hierarchy.ProductHierarchy;
import org.osdtsystem.hierarchy.SuffixHierarchy;
import org.osdtsystem.hierarchy.TreeHierarchy;
import org.osdtsystem.hierarchy.WordHierarchy;

/**
 * A class for reading hierarchy files in the native XHPM text format.
 * @author Matthias Buch-Kromann
 */
class XhpmTextFormatHierarchyIO extends AbstractHierarchyIO implements HierarchyIO {
    XhpmTextFormatHierarchyIO() {
    }

    @Override
    public Hierarchy<?> read(HierarchyManager manager, Reader rawReader) throws IOException {
        BufferedReader reader = new BufferedReader(rawReader);

        // Read first line
        String hierarchyType = reader.readLine();
        return null;
    }

    // General reader

    // Reader for suffix hierarchies

    // Writer for suffix hierarchies
    Hierarchy<?>[] write(HierarchyManager manager, SuffixHierarchy hierarchy, Writer writer) throws IOException {
        writer.append("SuffixHierarchy\n");
        writer.close();
        return new Hierarchy<?>[0];
    }

    // Writer for prefix hierarchies
    Hierarchy<?>[]  write(HierarchyManager manager, PrefixHierarchy hierarchy, Writer writer) throws IOException {
        writer.append("PrefixHierarchy\n");
        writer.close();
        return new Hierarchy<?>[0];
    }

    // Writer for prefix hierarchies
    Hierarchy<?>[]  write(HierarchyManager manager, WordHierarchy hierarchy, Writer writer) throws IOException {
        writer.append("WordHierarchy\n");
        writer.close();
        return new Hierarchy<?>[0];
    }

    // Writer for product hierarchies
    Hierarchy<?>[] write(HierarchyManager manager, ProductHierarchy hierarchy, Writer writer) throws IOException {
        // Write hierarchy type
        writer.append("ProductHierarchy\n");

        // Write subhierarchies
        boolean first = true;
        Hierarchy<?>[] subhierarchies = new Hierarchy<?>[hierarchy.components()];
        for (int i = 0; i < hierarchy.components(); ++i) {
            // Write separator
            if (! first)
                writer.append(" ");

            // Write name of subhierarchy
            subhierarchies[i] = hierarchy.componentHierarchy(hierarchy.componentType(i), i);
            writer.append("\"");
            writer.append(manager.hierarchyName(subhierarchies[i]));
            writer.append("\"");
        }
        writer.append("\n");
        writer.close();

        // Return subhierarchies
return subhierarchies;
    }

    // Writer for tree hierarchies
    public void write(TreeHierarchy hierarchy, Writer writer) {

    }

}
